package com.thinkgem.jeesite.modules.lottery.mobile;

import com.thinkgem.jeesite.common.utils.Global;
import com.thinkgem.jeesite.common.utils.Result;
import com.thinkgem.jeesite.modules.lottery.entity.Activity;
import com.thinkgem.jeesite.modules.lottery.entity.Participation;
import com.thinkgem.jeesite.modules.lottery.entity.WechatUser;
import com.thinkgem.jeesite.modules.lottery.service.ActivityService;
import com.thinkgem.jeesite.modules.lottery.service.ParticipationService;
import com.thinkgem.jeesite.modules.lottery.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/lottery/wechatUser")
public class WechatUserMobileController {

    @Autowired
    private WechatUserService wechatUserService;
    @Autowired
    private ParticipationService participationService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(WechatUser wechatUser) {
        WechatUser user = wechatUserService.findByOpenid(wechatUser.getOpenid());
        if (user == null) {
            wechatUser.setActorId("0");
            wechatUserService.save(wechatUser);
        }

        Result result = Result.ok("登录成功");
        information(result, wechatUser.getOpenid());
        return result;
    }

    @RequestMapping(value = "getUserInformation", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserInformation(String openid) {
        WechatUser user = wechatUserService.findByOpenid(openid);
        if (user == null) {
            return Result.error("请先给小程序授权");
        }

        Result result = Result.ok("用户信息获取成功");
        information(result, openid);
        return result;
    }

    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserListOfActivity(String activityId) {
        List<WechatUser> wechatUsers = wechatUserService.getUserListOfActivity(activityId);

        Result result = Result.ok("活动的用户列表获取成功");
        result.put("userList", wechatUsers);
        return result;
    }

    @RequestMapping(value = "getActivityStatus", method = RequestMethod.POST)
    @ResponseBody
    public Result getActivityStatus(String openid, String activityId) {
        Result result = Result.ok("用户与活动的关系获取成功");
        Activity activity = activityService.get(activityId);
        if (activity != null) {
            result.put("status", Global.OPEN);
        } else {
            Participation participation = participationService.getUserParticipation(openid, activityId);
            if (participation == null) {
                result.put("status", Global.NOJOIN);
            } else if (participation.getPrizeclassId() == null) {
                result.put("status", Global.NOTPRIZE);
            } else {
                result.put("status", Global.PRIZE);
            }
        }
        return result;
    }

    private void information(Result result, String openid) {
        result.put("attendlottery", participationService.getNumOfActivity(openid));
        result.put("makelottery", activityService.getUserNumOfActivity(openid));
        result.put("luckylottery", participationService.getUserNumOfPrize(openid));
    }

}
